package com.goldcn.common;

import com.goldcn.common.model.DataResult;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.order.erpVo.ResultData;
import com.google.common.base.Strings;
import net.sf.json.JSONObject;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * Created by Administrator on 2017/7/17.
 */
public class HttpUtil {
    public static DataResult httpGet(String url,RestTemplate restTemplate){
        return httpGet(url,restTemplate,true);
    }

    public static <T> DataResult httpPost(String url, T body,RestTemplate restTemplate){
        return httpPost(url,body,restTemplate,true);
    }

    public static JSONObject httpGetJSONObject(String url, RestTemplate restTemplate){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpEntity<String> entity = new HttpEntity<String>("",getHttpHeaders(true));
        ResponseEntity<JSONObject> result = restTemplate.exchange(url, HttpMethod.GET, entity,JSONObject.class , "");
        return result.getBody();
    }


    //get请求返回resultData
    public static ResultData httpGet_ResultData(String url, RestTemplate restTemplate){
        return httpGet_ResultData(url,restTemplate,true);
    }


   /* public static DataResult httpGet(String url){
        return httpGet(url,null,false);
    }

    public static <T> DataResult httpPost(String url, T body){
        return httpPost(url,body,null,false);
    }*/

    private static ResultData httpGet_ResultData(String url,RestTemplate restTemplate,boolean containHeaders){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpEntity<String> entity = new HttpEntity<String>("",getHttpHeaders(containHeaders));
        ResponseEntity<ResultData> result = restTemplate.exchange(url, HttpMethod.GET, entity, ResultData.class, "");
        return result.getBody();
    }


    //需要手动传入公司ID，解决request里面没有companyID的情况
    public static ResultData httpGet_ResultData_headers(String url,RestTemplate restTemplate,Long companyID){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
        headers.setContentType(mediaType);
        headers.set("companyID",companyID+"");
        headers.set("ver","2.0");
        HttpEntity<String> entity = new HttpEntity<String>("",headers);
        ResponseEntity<ResultData> result = restTemplate.exchange(url, HttpMethod.GET, entity, ResultData.class, "");
        return result.getBody();
    }

    private static DataResult httpGet(String url,RestTemplate restTemplate,boolean containHeaders){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpEntity<String> entity = new HttpEntity<String>("",getHttpHeaders(containHeaders));
        ResponseEntity<DataResult> result = restTemplate.exchange(url, HttpMethod.GET, entity, DataResult.class, "");
        return result.getBody();
    }



    private static <T> DataResult httpPost(String url, T body,RestTemplate restTemplate,boolean containHeaders){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpEntity<T> entity = new HttpEntity<T>(body,getHttpHeaders(containHeaders));
        ResponseEntity<DataResult> result = restTemplate.exchange(url, HttpMethod.POST, entity, DataResult.class, "");
        return result.getBody();
    }

    //需要手动传入公司ID，解决request里面没有companyID的情况
    public static <T> DataResult httpPost_headers(String url, T body,RestTemplate restTemplate,Long companyID){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
        headers.setContentType(mediaType);
        headers.set("companyID",companyID+"");
        headers.set("ver","2.0");
        HttpEntity<T> entity = new HttpEntity<T>(body,headers);
        ResponseEntity<DataResult> result = restTemplate.exchange(url, HttpMethod.POST, entity, DataResult.class, "");
        return result.getBody();
    }

    public static <T> JSONObject httpPOSTJSONObject(String url, T body,RestTemplate restTemplate,boolean containHeaders){
        if(restTemplate == null){
            restTemplate = new RestTemplate();
        }
        HttpEntity<T> entity = new HttpEntity<T>(body,getHttpHeaders(containHeaders));
        ResponseEntity<JSONObject> result = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class, "");
        return result.getBody();
    }

    private static HttpHeaders getHttpHeaders(boolean containHeaders){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
        headers.setContentType(mediaType);
        if(containHeaders){
            /*if(!Strings.isNullOrEmpty(SystemContext.getRequest().getHeader("ver"))){
                headers.set("ver", SystemContext.getRequest().getHeader("ver"));
            }*/
            //临时修改
            headers.set("ver", "2.0");
            if(SystemContext.getCompanyID() != null){
                headers.set("companyID", String.valueOf(SystemContext.getCompanyID()));
            }
        }
        return headers;
    }
}
